
{extend name="admin@publics/base"/}
{block name="title"}用户管理{/block}
{block name="body"}

<script>
//页面变量
_PAGE['adminUser'] = {
	_ID:0
};
/*==========控制器==========*/
Ext.define('admin.user.CURDViewController', {
	extend: 'Tg.tx.CURDViewController',
	alias: 'controller._adminUserController',
    //角色管理
	_ColumnRole:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Pk = _VM.get('_Pk');
        var _Select = _Grid.getStore().getAt(_Rindex);
		if(_Select.get('id') == ''){
			Ext.Msg.alert('错误', _Select.get('title')+' 主键为空');
			return false;
		}
		_PAGE['adminUser']['_ID'] = _Select.get('id');
        var _Win = Ext.create('Tg.tx.Window',{
			width:560,
			height:540,
			layout:'fit',
			title: _Select.get('truename')+' [ '+_Select.get('username')+' ] 的角色',
			items:{xtype:'admin_UserRole_index'}
        });
		_Win.show();
	},
});
/*==========模型==========*/
Ext.define('admin.user.CURDViewModel', {
	extend: 'Tg.tx.CURDViewModel',
	alias: 'viewmodel._adminUserViewModel',
    //Data
    data:{
    	//权限
        _Auth:{
        	index	: {if condition="_CheckAuth('admin/User/index')"}true{else/}false{/if},
        	add		: {if condition="_CheckAuth('admin/User/add')"}true{else/}false{/if},
        	edit	: {if condition="_CheckAuth('admin/User/edit')"}true{else/}false{/if},
        	del		: {if condition="_CheckAuth('admin/User/del')"}true{else/}false{/if},
        	role	: {if condition="_CheckAuth('admin/UserRole/index')"}true{else/}false{/if},
        },
        _Url:{
        	index	: _PATH['site']+'/admin/user/index',
        	add		: _PATH['site']+'/admin/user/add',
        	edit	: _PATH['site']+'/admin/user/edit',
        	del		: _PATH['site']+'/admin/user/del',
        },
        //操作视图
        _Ext:{
        	index	: 'admin.user.index',
        	serch	: 'admin.user.serch',
        	add		: 'admin.user.updata',
        	edit	: 'admin.user.updata',
        	del		: 'admin.user.del',
        },
		_Pk:'id',
    },
    //Store
	stores: {
		_MainStore: {
			sorters: [{property: 'id',direction: 'desc'}],
			pageSize: 20,
			remoteSort: true,
			remoteFilter:true,
			proxy: {
				type: 'ajax',
				api: {read:_PATH['site']+'/admin/user/index'},
				actionMethods:{read: "POST"},
				reader: {type: 'json',rootProperty: 'rows',totalProperty: 'total'}
			},
		}
	},
});
/*==========视图==========*/
//列表
Ext.define('admin.user.index', {
	extend: 'Tg.tx.CURDList',
	alias: 'widget.admin_User_index',
	requires: [
		'admin.user.CURDViewController',
		'admin.user.CURDViewModel',
	],
	controller: '_adminUserController',
	viewModel: '_adminUserViewModel',
    _TbarLBefore:[
        {xtype: 'label',text: '用户管理',itemId:'_Tit',style: 'font-size:16px; padding-right:10px;margin-right: 10px;border-right: dotted 1px #ccc;'},
    ],
    items:[
		{
			xtype:'CURDListGrid',
			bind:{store:'{_MainStore}'},
			columns:[
        		{xtype:"actioncolumn",header:"角色",width: 50,iconCls:'tf tf-plug',align:'center',locked: true,handler: '_ColumnRole',bind:{hidden:'{!_Auth.role}'}},
        		{xtype:"actioncolumn",header:"更新",width: 50,iconCls:'tf tf-edit',align:'center',locked: true,handler: '_ColumnEdit',bind:{hidden:'{!_Auth.edit}'}},
				{xtype:"actioncolumn",header:"删除",width: 50,iconCls:'tf tf-del',align:'center',locked: true,handler: '_ColumnDel',bind:{hidden:'{!_Auth.del}'}},
				{dataIndex:'id', header :'主键', width :60, hidden:false, locked:true, sortable:true },
				{text: '头像',width: 60,sortable: false,locked: true,renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {
					if (record.data.avatar) {
						return "<img src='" + record.data.avatar + "' style='width:40px; height:40px;'/>";
					} else {
						return "<img src='" + _PATH['upload'] + "/avatar.png' style='width:40px; height:40px;'/>";
					}
				}},
				{text: '账户',width: 160,sortable: false,locked: true,renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {
					return "<strong style='width: 60px; text-align: right; display: inline-block'>用户名：</strong>" + record.data.username + "<br/><strong style='width: 60px; text-align: right; display: inline-block'>姓名：</strong>" + record.data.truename
				}},
				{text: '联系方式',width: 260,sortable: false,renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {
					return "<strong style='width: 40px; text-align: right; display: inline-block'>邮箱：</strong>" + record.data.email + "<br/><strong style='width: 40px; text-align: right; display: inline-block'>电话：</strong>" + record.data.phone
				}},
				{text: '登录信息',width: 240,sortable: false,renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {
					return "<strong style='width: 40px; text-align: right; display: inline-block'>次数：</strong>" + record.data.login_num + "<br/><strong style='width: 40px; text-align: right; display: inline-block'>本次：</strong>" + record.data.login_ip + " [" + record.data.login_time + "]<br/><strong style='width: 40px; text-align: right; display: inline-block'>历史：</strong>" + record.data.old_login_ip + " [" + record.data.old_login_time + "]"
				}},
				{text: '状态',dataIndex: 'status',renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_Dictionary,'sys_status', value)}},
				{text: '操作时间',width: 220,sortable: false,renderer: function(value, cellmeta, record, rowIndex, columnIndex, store) {
					return "<strong style='width: 40px; text-align: right; display: inline-block'>创建：</strong>" + record.data.create_time + "<br/><strong style='width: 40px; text-align: right; display: inline-block'>更新：</strong>" + record.data.update_time
				}},
			],
		}
	],
});

//搜索
Ext.define('admin.user.serch', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_User_serch',
	requires: [
		'admin.user.CURDViewController',
		'admin.user.CURDViewModel',
	],
	controller: '_adminUserController',
	viewModel: '_adminUserViewModel',
    title:'搜索',
	width:500,
	height:420,
    items:{
    	xtype:'CURDForm',
        items:[ 
			{items: [
				{fieldLabel: '主键', name: 's_id',xtype: "textfield"},
			]},
			{items : [
				{name: 's_username',fieldLabel: '用户名',columnWidth: .5},
				{name: 's_truename',fieldLabel: '姓名',columnWidth: .5},
			]},
			{items : [
				{name: 's_phone',fieldLabel: '电话',columnWidth: .5},
				{name: 's_email',fieldLabel: '邮箱',columnWidth: .5},
			]},
			{items : [
				{name: 's_status',xtype: "combobox",editable:false,fieldLabel: '状态',displayField: "text",valueField: "value",store: {fields: ["text", "value"],data:_Dictionary['sys_status']}},
			]},
			{items: [
				{fieldLabel: '起始创建时间', name: 's_create_time_min',xtype: "datetimefield",columnWidth: .5},
				{fieldLabel: '截止创建时间', name: 's_create_time_max',xtype: "datetimefield",columnWidth: .5},
			]},
			{items: [
				{fieldLabel: '起始更新时间', name: 's_update_time_min',xtype: "datetimefield",columnWidth: .5},
				{fieldLabel: '截止更新时间', name: 's_update_time_max',xtype: "datetimefield",columnWidth: .5},
			]},
        ]
    }
});



//操作 
Ext.define('admin.user.updata', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_User_updata',
	requires: [
		'admin.user.CURDViewController',
		'admin.user.CURDViewModel',
	],
	controller: '_adminUserController',
	viewModel: '_adminUserViewModel',
    title:'用户 - 操作',
	width:500,
	height:330,
    items:{
    	xtype:'CURDForm',
        items:[
			{items : [
				{name: 'username',fieldLabel: '用户名',columnWidth: .5},
				{name: 'truename',fieldLabel: '姓名',columnWidth: .5},
			]},
			{items : [
				{name: 'password',fieldLabel: '密码',columnWidth: .5},
			]},
			{items : [
				{name: "avatar",fieldLabel: '头像',xtype: 'keimg'},
			]},
			{items : [
				{name: 'phone',fieldLabel: '电话',columnWidth: .5},
				{name: 'email',fieldLabel: '邮箱',columnWidth: .5},
			]},
			{items : [
				{xtype: "combobox",editable:false,name: 'status',columnWidth :1,fieldLabel: '状态',displayField: "text",valueField: "value",store: {fields: ["text", "value"],data:_Dictionary['sys_status']}},
			]},
			{xtype:'hiddenfield', fieldLabel: '主键', name: 'id', itemId: 'id', cls: '', columnWidth: 1, height: 30, readOnly: false},
        ]
    }
});



//实例化
Ext.application({
	name: 'admin',
	extend: 'Ext.app.Application',
	autoCreateViewport: 'admin.user.index'
});

/*==========控制器==========*/
Ext.define('admin.user_role.CURDViewController', {
	extend: 'Tg.tx.CURDViewController',
	alias: 'controller._adminUserRoleController',
	//新增
	_Add:function(button, e, eOpts){
		var _Me = this;
		if(!_Me._CheckAuth('add')){return false;}
		var _VM = _Me.getView().getViewModel();
		var _Auth = _VM.get('_Auth.add');
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.add')+'/user_id/'+_PAGE['adminUser']['_ID'];
		var _Ext = _VM.get('_Ext.add');
        var _Win = Ext.create(_Ext,{
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		Ext.create('Tg.tx.Action').getDataToForm({form:_Win.down("form").getForm(),url:_Url})
	},
});
/*==========模型==========*/
Ext.define('admin.user_role.CURDViewModel', {
	extend: 'Tg.tx.CURDViewModel',
	alias: 'viewmodel._adminUserRoleViewModel',
    //Data
    data:{
    	//权限
        _Auth:{
        	index	: {if condition="_CheckAuth('admin/UserRole/index')"}true{else/}false{/if},
			serch	: false ,
        	add		: {if condition="_CheckAuth('admin/UserRole/add')"}true{else/}false{/if},
        	edit	: {if condition="_CheckAuth('admin/UserRole/edit')"}true{else/}false{/if},
        	del		: {if condition="_CheckAuth('admin/UserRole/del')"}true{else/}false{/if},
        },
        _Url:{
        	index	: _PATH['site']+'/admin/user_role/index',
        	add		: _PATH['site']+'/admin/user_role/add',
        	edit	: _PATH['site']+'/admin/user_role/edit',
        	del		: _PATH['site']+'/admin/user_role/del',
        },
        //操作视图
        _Ext:{
        	index	: 'admin.user_role.index',
        	serch	: 'admin.user_role.serch',
        	add		: 'admin.user_role.updata',
        	edit	: 'admin.user_role.updata',
        	del		: 'admin.user_role.del',
        },
		_Pk:'id',
    },
    //Store
	stores: {
		_MainStore: {
			sorters: [{property: 'id',direction: 'desc'}],
			pageSize: 20,
			remoteSort: true,
			remoteFilter:true,
			proxy: {
				type: 'ajax',
				api: {read:_PATH['site']+'/admin/user_role/index'},
				actionMethods:{read: "POST"},
				reader: {type: 'json',rootProperty: 'rows',totalProperty: 'total'}
			},
		}
	},
});
/*==========视图==========*/
//列表
Ext.define('admin.user_role.index', {
	extend: 'Tg.tx.CURDList',
	alias: 'widget.admin_UserRole_index',
	requires: [
		'admin.user_role.CURDViewController',
		'admin.user_role.CURDViewModel',
	],
	controller: '_adminUserRoleController',
	viewModel: '_adminUserRoleViewModel',
	initComponent:function(){
        var _Me = this;
        var _VM = _Me.getViewModel();
		_VM.set('_MainStore.proxy.api.read',_PATH['site']+'/admin/user_role/index/user_id/'+_PAGE['adminUser']['_ID']);
        _Me.callParent(arguments);
	},
    items:[
		{
			xtype:'CURDListGrid',
			bind:{store:'{_MainStore}'},
			columns:[
        		{xtype:"actioncolumn",header:"更新",width: 50,iconCls:'tf tf-edit',align:'center',locked: true,handler: '_ColumnEdit',bind:{hidden:'{!_Auth.edit}'}},
				{xtype:"actioncolumn",header:"删除",width: 50,iconCls:'tf tf-del',align:'center',locked: true,handler: '_ColumnDel',bind:{hidden:'{!_Auth.del}'}},
				{dataIndex:'id', header :'主键', width :60, hidden:true, locked:true, sortable:true },
				{dataIndex:'def', header :'默认', width :50, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_Dictionary,'sys_true', value);}},
				{dataIndex:'group_id_show', header :'角色', width :350, hidden:false, locked:false, sortable:false},
				{dataIndex:'create_time', header :'创建时间', width :110, hidden:true, locked:false, sortable:true },
				{dataIndex:'update_time', header :'更新时间', width :110, hidden:true, locked:false, sortable:true },
			],
		}
	],
});


//操作 
Ext.define('admin.user_role.updata', {
	extend: 'Tg.tx.Window',
	alias: 'widget.admin_UserRole_updata',
	requires: [
		'admin.user_role.CURDViewController',
		'admin.user_role.CURDViewModel',
	],
	controller: '_adminUserRoleController',
	viewModel: '_adminUserRoleViewModel',
    title:'角色 - 操作',
	width:500,
	height:240,
    items:{
    	xtype:'CURDForm',
        items:[
			{items: [
				{fieldLabel: '角色', name: 'group_id',columnWidth :1,xtype:'treepicker',displayField: "text",valueField: "id",rootVisible:true,store:Ext.create('Ext.data.TreeStore',{autoLoad: true,root:{expanded:true,},proxy : {type : 'ajax',url : _PATH['site']+'/admin/api/group_lists/rtype/1?verify_code={:_AutoCodeUrl('admin/AuthRule/index')}'}})},
			]},
			{items: [
				{xtype:'combobox', fieldLabel: '默认', name: 'def', itemId: 'def', cls: '', height: 30, readOnly: false, emptyText:'请选择', editable:false, displayField:'text', valueField: 'value', store:{fields:['text','value'],data:_Dictionary['sys_true']}},
			]},
			{xtype:'hiddenfield', fieldLabel: '主键', name: 'id', itemId: 'id', cls: '', columnWidth: 1, height: 30, readOnly: false},
			{xtype:'hiddenfield', fieldLabel: '用户ID', name: 'uid', itemId: 'uid', cls: '', columnWidth: 1, height: 30, readOnly: false},
        ]
    }
});
</script>

{/block}
